package com.tajiang.business.mess.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.http.BaseResponse;
import com.tajiang.business.common.http.HttpHandler;
import com.tajiang.business.common.http.HttpResponseListener;
import com.tajiang.business.common.utils.CashUtils;
import com.tajiang.business.common.utils.LogUtils;
import com.tajiang.business.common.utils.MD5Utils;
import com.tajiang.business.common.utils.ToastUtils;
import com.tajiang.business.common.widget.LoadingDialog;
import com.tajiang.business.mess.dialog.WithDrawCashDialog;
import com.tajiang.business.model.Bank;
import com.tajiang.business.setting.activity.ResetPsdActivity;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 提现
 * Created by work on 2016/6/24.
 */
public class WithdrawBalanceActivity extends BaseActivity {

    public static final BigDecimal DefaultCashFee = new BigDecimal(2.0);  //提现银行收取的默认手续费
    public static final BigDecimal MIN_WITHDRAW_CASH = new BigDecimal(5.0);   //5元为最低提现金额

    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.et_withdraw_money)
    EditText etWithdrawMoney;
    @BindView(R.id.et_withdraw_password)
    EditText etWithdrawPassword;
    @BindView(R.id.tv_withdraw_money_limited)
    TextView tvWithdrawMoneyLimited;
    @BindView(R.id.tv_open_bank)
    TextView tvOpenBank;

    private BigDecimal withdrawCash;

    private Double limitedCash = 0.0;//提现金额上限
    private LoadingDialog loadingDialog;

    private void initMyView() {
        this.loadingDialog = new LoadingDialog(this);
        if (getIntent().hasExtra("bank_name")) {
            tvOpenBank.setText(getIntent().getStringExtra("bank_name"));
        }
        if (getIntent().hasExtra("balance_cash")) {
            String money = getIntent().getStringExtra("balance_cash");
            tvWithdrawMoneyLimited.setText(money);
            limitedCash = Double.valueOf(money);
        }
    }

    @Override
    protected void initTopBar() {
        setTitle("提现");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_withdraw_balance);
        ButterKnife.bind(this);
        initMyView();
        initListener();
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.btn_confirm)
    public void onConfirmClick() {
        // 补足金额格式
        etWithdrawMoney.setText(CashUtils.CheckCashNumber(etWithdrawMoney.getText().toString()));
        if (isWithdrawCashEnable() == true) {
            checkPasswordEnable();
        }
    }

    private void initListener() {
        etWithdrawMoney.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    // 此处为得到焦点时的处理内容
                } else {
                    // 此处为失去焦点时的处理内容
                    etWithdrawMoney.setText(CashUtils.CheckCashNumber(etWithdrawMoney.getText().toString()));
                }
            }
        });
    }

    /**
     * 判断是否设置过提现密码
     */
    private void checkPasswordEnable() {
        loadingDialog.show();
        new HttpHandler(new HttpResponseListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                Bank bank = (Bank)response.getData();
                if (bank.getPassword().equals("true")){
                    WithdrawBalanceActivity.this.withdrawCash = new BigDecimal(
                            Double.valueOf(etWithdrawMoney.getText().toString()));
                    showCashDialog();
                } else {
                    //跳转到设置提现密码的页面
                    ToastUtils.showShort("请先设置提现密码");
                    intent2Activity(ResetPsdActivity.class);
                }
            }

            @Override
            public void onFailed(BaseResponse response) {

            }

            @Override
            public void onFinish() {
                if (loadingDialog.isShowing()) {
                    loadingDialog.dismiss();
                }
            }
        }).getCeoUserIdBank();
    }

    private boolean isWithdrawCashEnable() {
        try {
            if (etWithdrawMoney.getText().toString().equals("")) {
                ToastUtils.showShort("请输入提现金额");
                return false;
            }
            Double cash = Double.valueOf(etWithdrawMoney.getText().toString());
            if (cash > limitedCash) {
                ToastUtils.showShort("余额不足，请重新输入");
                return false;
            } else if (cash < MIN_WITHDRAW_CASH.doubleValue()) {
                ToastUtils.showShort("5元为最低提现金额");
                return false;
            }
            if (etWithdrawPassword.getText().toString().equals("")) {
                ToastUtils.showShort("请输入提现密码");
                return false;
            }
        } catch (NumberFormatException  e) {
            LogUtils.e(e.toString());
            ToastUtils.showShort("请输入正确金额格式！");
            return false;
        }
        return true;
    }

    /**
     *
     * 显示确认对话框
     * */
    private void showCashDialog() {
        WithDrawCashDialog dialog = new WithDrawCashDialog(this);
        dialog.setWithdrawCash(CashUtils.getCashWith2Point(
                new BigDecimal(withdrawCash.doubleValue() - DefaultCashFee.doubleValue())));
        dialog.setRequestListener(new WithDrawCashDialog.RequestListener() {
            @Override
            public void doPostRequest() {
                PostCashRequest();  //提现请求
            }
        });
        dialog.show();
    }

    private void PostCashRequest() {
        loadingDialog.show();
        new HttpHandler(new HttpResponseListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                if ((Boolean) response.getData() == true) {
                    Intent intent = new Intent();
                    intent.putExtra("withdraw_cash",CashUtils.getCashWith2Point(
                            new BigDecimal(withdrawCash.doubleValue() - DefaultCashFee.doubleValue())));
                    intent2ActivityWithExtras(intent, WithdrawProgressActivity.class);
                    WithdrawBalanceActivity.this.finish();
                } else {
                    ToastUtils.showShort(response.getMoreInfo());
                }
            }

            @Override
            public void onFailed(BaseResponse response) {

            }

            @Override
            public void onFinish() {
                if (loadingDialog.isShowing()) {
                    loadingDialog.dismiss();
                }
            }
        }).addApply(Float.valueOf(CashUtils.getCashWith2Point(
                new BigDecimal(withdrawCash.doubleValue()))),
                MD5Utils.StringMD5(etWithdrawPassword.getText().toString()));
    }


//    private void showWithdrawDialog() {
//        final AlertDialog dialog;
//        AlertDialog.Builder builder=new AlertDialog.Builder(this);  //先得到构造器
//        View myView = this.getLayoutInflater().inflate(R.layout.layout_withdrawbalance_dialog, null);
//        TextView tvWithdrawMoney = (TextView) myView.findViewById(R.id.tv_withdraw_money);
//        TextView tv_ensure = (TextView) myView.findViewById(R.id.btn_ensure);
//        TextView tv_cancel = (TextView) myView.findViewById(R.id.btn_cancel);
//
//        builder.setView(myView);
//        builder.setCancelable(false);
//        dialog = builder.create();
//
//        double money = Double.valueOf(etWithdrawMoney.getText().toString());
//        final double withdrawCash = money - 2.0;
//        tvWithdrawMoney.setText(String.valueOf(withdrawCash));
//        tv_ensure.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                dialog.dismiss();
//                PostCashRequest();  //提现请求
//            }
//        });
//        tv_cancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                dialog.dismiss();
//            }
//        });
//        /*
//         * 将对话框的大小按屏幕大小的百分比设置
//         */
//        Window dialogWindow = dialog.getWindow();
//        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
//        dialog.show();
//
//        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
//        int width = wm.getDefaultDisplay().getWidth();
//        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
//        params.width = (int) (width * 0.8);
//        dialog.getWindow().setAttributes(params);
//
//    }

}
